import numpy as np

from mainmethod2 import *

# import pandas as pd

if __name__ == '__main__':
    flag = 0
    # ecfinal = 10000
    ecfinal = np.ceil(0.05 * 512 * 512)
    k = 16
    key = 88
    Img = Load_img('test')
    print(Img.img_path)
    for j in range(0,len(Img.img_path)-4):
        img = Img[j]
        pathname = Img.img_path[j]
        name = pathname.split('.', 1)[0] + '.csv'
        imgs = copy.deepcopy(img)
        list = np.zeros(256)
        data = pd.read_csv('./dataset/'+name).values
        data1 = data[0]
        data1 = data1[1:]
        data2 =np.argsort(-data1)
        dataset =np.array(data2[:8],dtype=int)
        # img_hide, RateList, secretinfos, cm, PZlist,Rates,ec1 ,em,typemap,p,predx, v1, h1,secret_info= danci(imgs, flag, k, ecfinal)
        v, h = get_grad(imgs)
        for i in range(10, 255):
        # i = 79
            c = bin(i)
            # d =
            c = ''.join(c[2:])
            d = []
            d.__iadd__(c)
            d = np.array(d, dtype=int)
            if np.sum(d) > 4:
                print('此时为第{}轮'.format(i))
                img_hide, RateList, ec1 = danci(imgs, flag, k, ecfinal, key, i, v, h,dataset)
                new_img = copy.deepcopy(img_hide)

                print('ec', RateList, ec1)
                if int(np.sum(ec1)) == 0:
                    print('第二次插入')
                    k = 16
                    flags = 1
                    v2, h2 = get_grad(new_img)
                    img_hide, RateList, ec = danci(new_img, flags, k, ecfinal, key, i, v2, h2,dataset)
                    # pd.DataFrame(new_img).to_csv('./newimg.csv')
                    psnr = cal_psnr(img, img_hide, ec)
                    print('psnr', psnr)
                    list[i] = psnr
                    pd.DataFrame(list).to_csv('./2022-2-25/'+name)



# x_size = np.linspace(0.1, 0.5, 21)
# x_size = np.linspace(0.1, 0.7, 7)
# x_size = np.linspace(0.01,0.17,17)
# y_size = np.zeros(17)
# # # y_size = np.zeros(21)
# # # print(x_size)
# Img = Load_img('test')
# # for j in range(len(Img.img_path)):
# img = Img[5]
# pathname = Img.img_path[5]
# name = pathname.split('.', 1)[0] + '.csv'
# imgs = copy.deepcopy(img)
# index = 0
# key =3.5
# for i in range(len(x_size)):
#     flag = 0
#     print("第{}论".format(i))
#     ecfinal = np.ceil(x_size[i] * 512 * 512)
#     k = 16
#     img_hide, RateList,ec1 = danci(imgs, flag, k, ecfinal,key)
#     new_img = copy.deepcopy(img_hide)
#     print('ec', RateList)
#     if int(np.sum(ec1)) == 0:
#         print('第二次插入')
#         k = 16
#         flags = 1
#         new_img, RateLis2t, ec = danci(new_img, flags, k, ecfinal, key)
#             # pd.DataFrame(new_img).to_csv('./newimg.csv')
#         psnr = cal_psnr(img, new_img, ec)
#         print('psnr', psnr)
#         y_size[index] = psnr
#         index += 1
#     pd.DataFrame(y_size).to_csv('./dataset3/'+name)
# plt_make(x_size, y_size)
